package com.ysd.lis.controller.eq;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.eq.EqMaintainPlan;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.request.eq.EqMaintainDto;
import com.ysd.lis.service.eq.EqMaintainPlanService;
import com.ysd.util.CornUtil;
import io.swagger.annotations.ApiOperation;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 设备管理-设备类型 前端控制器
 * </p>
 *
 * @author prs
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/api/eq/eqMaintainPlan")
public class EqMaintainPlanController extends BaseController {

    @Autowired
    EqMaintainPlanService eqMaintainPlanService;

    @ApiOperation("分页查询")
    @GetMapping("/findMaintainPlanPageList")
    public Result findMaintainPlanPageList(CommonPageDto pageParam){
        return eqMaintainPlanService.findMaintainPlanPageList(pageParam);
    }

    @ApiOperation("添加")
    @PostMapping("/addMaintainPlan")
    public Result addMaintainPlan(@RequestBody EqMaintainPlan eqMaintainPlan) {
        return eqMaintainPlanService.addMaintainPlan(eqMaintainPlan);
    }

    @ApiOperation("编辑")
    @PostMapping("/updateMaintainPlan")
    public Result updateMaintainPlan(@RequestBody EqMaintainPlan eqMaintainPlan) {
        return eqMaintainPlanService.updateMaintainPlan(eqMaintainPlan);
    }

    @ApiOperation("根据ID查")
    @GetMapping("/findEqMaintainPlanById")
    public Result findEqMaintainPlanById( EqMaintainPlan eqMaintainPlan) {
        return eqMaintainPlanService.findEqMaintainPlanById(eqMaintainPlan);
    }

    @ApiOperation("批量删除")
    @PostMapping("/batchDeleteMaintainPlanList")
    public Result batchDeleteMaintainPlanList(@RequestBody EqMaintainDto dto){
        return  eqMaintainPlanService.batchDeleteMaintainPlanList(dto);
    }

    @ApiOperation("批量修改状态")
    @PostMapping("/batchMaintainPlanStopStart")
    public Result batchMaintainPlanStopStart(@RequestBody EqMaintainDto dto){
        return  eqMaintainPlanService.batchMaintainPlanStopStart(dto);
    }


    @GetMapping("/validateCron")
    public Result validateCron(String cron) {
        Map returnMap = new HashMap();
        List<String> dateList = new ArrayList<>();
        if (StringUtils.isBlank(cron)) {
            return Result.succ(400, "表达式为空！,请核实", "");
        }
        try {
            CronExpression cronExpression = new CronExpression(cron);
            //获取未来五次的执行时间
            Date nextInvalidTimeAfter = cronExpression.getNextValidTimeAfter(new Date());
            Date nextInvalidTimeAfter1 = cronExpression.getNextValidTimeAfter(nextInvalidTimeAfter);
            Date nextInvalidTimeAfter2 = cronExpression.getNextValidTimeAfter(nextInvalidTimeAfter1);
            Date nextInvalidTimeAfter3 = cronExpression.getNextValidTimeAfter(nextInvalidTimeAfter2);
            Date nextInvalidTimeAfter4 = cronExpression.getNextValidTimeAfter(nextInvalidTimeAfter3);
            dateList.add(DateUtil.format(nextInvalidTimeAfter, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(DateUtil.format(nextInvalidTimeAfter1, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(DateUtil.format(nextInvalidTimeAfter2, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(DateUtil.format(nextInvalidTimeAfter3, "yyyy-MM-dd HH:mm:ss"));
            dateList.add(DateUtil.format(nextInvalidTimeAfter4, "yyyy-MM-dd HH:mm:ss"));
            returnMap.put("dateList", dateList);
            //获取cron表达式的文字描述
            String des = CornUtil.descCorn(cron);
            returnMap.put("des", des);
        } catch (Exception e) {
            return Result.succ(400, "表达式错误,请核实", "");
        }
        return Result.succ(1, "成功", returnMap);
    }

}
